package com.example.android_streamer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android_streamer.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'android_streamer' library on application startup.
    static {
        System.loadLibrary("android_streamer");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
            // 申请 相机 麦克风权限
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.INTERNET,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            }, 100);
        }

        Button pushBtn = findViewById(R.id.push);
        EditText inputEt = findViewById(R.id.input);
        EditText outputEt = findViewById(R.id.output);
        pushBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String folder_url = "/sdcard";
                String input_url = inputEt.getText().toString();
                String input = folder_url + "/" + input_url;
                String output = outputEt.getText().toString();
                int ret = stream(input, output);
                if (ret == -1) {
                    Log.d("MainActivity", "error...");
                }else{
                    Log.d("MainActivity","push ok!");
                }
//                Log.e("MainActivity", input);
//                Log.e("MainActivity", output);
            }
        });
    }

    private native int stream(String input, String output);

    public native String stringFromJNI();

}